package com.huawei.updatesdk.service.c;

import android.content.ContextWrapper;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.fastengine.fastview.download.install.PackageManagerConstants;
import com.huawei.updatesdk.service.c.c.b;
import com.huawei.updatesdk.service.e.c;
import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class g {
    protected static final d a = new d();
    private static final ExecutorService b = Executors.newFixedThreadPool(1);

    public static final class a extends AsyncTask<Void, Void, Boolean> {
        private final String a;
        private final int b;
        private boolean c = false;
        private b d;

        private a(String str, int i, boolean z) {
            this.a = str;
            this.b = i;
            this.c = z;
        }

        private void a() {
            if (!TextUtils.isEmpty(this.d.d())) {
                File file = new File(this.d.d());
                if (file.exists() && !file.delete()) {
                    com.huawei.updatesdk.a.a.b.a.a.a.d(PackageManagerConstants.TAG, "file delete error.");
                }
            }
        }

        public static void a(String str, int i) {
            new a(str, i, false).execute(new Void[0]);
        }

        public static void a(String str, int i, boolean z) {
            new a(str, i, z).execute(new Void[0]);
        }

        private void a(String str, File file) {
            String str2 = str + File.separator + file.getName();
            File file2 = new File(str);
            if (!(file2.exists() || file2.mkdirs())) {
                com.huawei.updatesdk.a.a.b.a.a.a.d(PackageManagerConstants.TAG, "getbackFile mkdirs failed!");
            }
            file2 = new File(str2);
            if (f.a(file, file2)) {
                File parentFile = file2.getParentFile();
                if (parentFile != null) {
                    parentFile.setExecutable(true, false);
                }
                file2.setReadable(true, false);
                this.d.b(str2);
                str2 = "install|pkg:" + this.d.c() + "|path:" + this.d.d();
                Thread thread = new Thread(new e(com.huawei.updatesdk.a.b.a.a.a().b(), this.d));
                thread.setName(str2);
                thread.start();
            } else if (b()) {
                this.c = false;
            } else {
                g.a(5, 0);
            }
        }

        private boolean a(String str) {
            this.d = g.b(str);
            if (this.d == null) {
                return false;
            }
            if (1 == this.b) {
                if (TextUtils.isEmpty(this.d.d())) {
                    return false;
                }
                g.a.remove(b.INSTALL);
                return c.a(this.d.d());
            } else if (this.c) {
                this.d.b(this.d.f() + 1);
                if (this.d.f() >= h.a().length) {
                    com.huawei.updatesdk.a.a.b.a.a.a.d(PackageManagerConstants.TAG, "getRetryBackupPaths large,can not change file path to retry install");
                    this.c = false;
                    return false;
                }
                String str2 = h.a()[this.d.f()];
                File file = new File(this.d.d());
                if (file.exists()) {
                    a(str2, file);
                    return false;
                }
                com.huawei.updatesdk.a.a.b.a.a.a.d(PackageManagerConstants.TAG, "file do not exist,can not change file path to retry install");
                this.c = false;
                return false;
            } else {
                a();
                return false;
            }
        }

        private boolean b() {
            return VERSION.SDK_INT < 23 || -1 != new ContextWrapper(com.huawei.updatesdk.a.b.a.a.a().b()).checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE");
        }

        protected Boolean a(Void... voidArr) {
            boolean a = a(this.a);
            if (!(this.d == null || b.INSTALL != this.d.e() || 1 == this.b || this.c || this.d.f() <= -1)) {
                a();
            }
            return Boolean.valueOf(a);
        }

        protected void a(Boolean bool) {
            super.onPostExecute(bool);
        }

        protected /* synthetic */ Object doInBackground(Object[] objArr) {
            return a((Void[]) objArr);
        }

        protected /* synthetic */ void onPostExecute(Object obj) {
            a((Boolean) obj);
        }
    }

    private static b a(String str, com.huawei.updatesdk.service.c.d.a aVar) {
        b a = a.a(str, aVar);
        return a != null ? a : null;
    }

    public static void a(int i, int i2) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putInt("INSTALL_STATE", i);
        bundle.putInt("INSTALL_TYPE", i2);
        intent.putExtras(bundle);
        com.huawei.updatesdk.service.b.c.a().c(com.huawei.updatesdk.a.b.c.b.a(intent));
    }

    public static void a(String str, String str2) {
        b(str, str2);
    }

    private static b b(String str) {
        return a(str, com.huawei.updatesdk.service.c.d.a.INSTALL_TYPE);
    }

    private static synchronized void b(String str, String str2) {
        synchronized (g.class) {
            com.huawei.updatesdk.a.a.b.a.a.a.a(PackageManagerConstants.TAG, "process:processType=install,path=" + str + ",packageName:" + str2 + ",isNow=" + false);
            if (TextUtils.isEmpty(str)) {
                com.huawei.updatesdk.a.a.b.a.a.a.d(PackageManagerConstants.TAG, "install failed!!!path is empty!!!!");
            } else {
                b bVar = new b(str2, str);
                bVar.a(com.huawei.updatesdk.service.c.c.a.WAIT_INSTALL);
                bVar.a(false);
                a.a(str2, bVar);
                bVar.a(b.INSTALL);
                String str3 = "install|pkg:" + bVar.c() + "|path:" + bVar.d();
                Thread thread = new Thread(new e(com.huawei.updatesdk.a.b.a.a.a().b(), bVar));
                thread.setName(str3);
                thread.start();
            }
        }
    }
}
